package com.footprint.common.httpclient;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

public class HttpClientSample {
	
	public void sample () throws URISyntaxException {
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		qparams.add(new BasicNameValuePair("q", "httpclient"));
		qparams.add(new BasicNameValuePair("btnG", "Google Search"));
		qparams.add(new BasicNameValuePair("aq", "f"));
		qparams.add(new BasicNameValuePair("oq", null));
		URI uri = URIUtils.createURI("http", "www.google.com", -1, "/search", 
		    URLEncodedUtils.format(qparams, "UTF-8"), null);
		
		

		/*
				HttpResponse response;
				try {
					response = httpClient.execute(httpget);
					
					// header
					System.out.println(response.getProtocolVersion());
					System.out.println(response.getStatusLine().getStatusCode());
					System.out.println(response.getStatusLine().getReasonPhrase());
					System.out.println(response.getStatusLine().toString());
					displaySeperator();

					// header
					HeaderIterator it = response.headerIterator("Set-Cookie");

					while (it.hasNext()) {
					    System.out.println(it.next());
					}
					displaySeperator();

					// HttpEntity
					HttpEntity entity = response.getEntity();
					if (entity != null) {
					    InputStream instream = entity.getContent();
					    InputStreamReader isr = new InputStreamReader(instream);
					    BufferedReader br = new BufferedReader(isr);
					    String s;
					    int line = 0;
					    String preStr = "formManager.submitExternal({url: '";
					    while((s = br.readLine()) != null && (++line < 3000)) {
					    	// System.out.println("Line:" + line + "\t " + s);
					    	// System.out.println(s);
					    	s = s.trim();
					    	int begin = s.indexOf("formManager");
					    	int end = s.indexOf("', failureUrl:");
					    	// System.out.println(begin);
					    	if(begin >= 0) {
					    		s = s.substring(preStr.length(), end);
					    		System.out.println(s);
					    		HttpGet httpget2 = new HttpGet(s);
					    		HttpResponse response2 = httpClient.execute(httpget2);
					    		HttpEntity entity2 = response2.getEntity();
								if (entity2 != null) {
								    InputStream instream2 = entity2.getContent();
								    InputStreamReader isr2 = new InputStreamReader(instream2);
								    BufferedReader br2 = new BufferedReader(isr2);
								    while((s = br2.readLine()) != null && (++line < 3000)) {
								    	System.out.println(s);
								    	
								    }
//								    int l;
//								    byte[] tmp = new byte[2048];
//								    while ((l = instream.read(tmp)) != -1) {
//								    	System.out.println("Line:" + l + "\t " + new String(tmp));
//								    }
								    br.close();
								    isr.close();
								    instream.close();
								}
					    		System.out.println(s+"\n"+s.substring(preStr.length(), end));
					    	}
					    }
//					    int l;
//					    byte[] tmp = new byte[2048];
//					    while ((l = instream.read(tmp)) != -1) {
//					    	System.out.println("Line:" + l + "\t " + new String(tmp));
//					    }
					    br.close();
					    isr.close();
					    instream.close();
					}
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				*/
	}
	
}
